package edu.gatech.asthma.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import edu.gatech.asthma.R;
import edu.gatech.asthma.database.ContactVO;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.ui_support.ContactMenu;
import edu.gatech.asthma.ui_support.HelpMenuHelper;


/**
 * Dislays all emergency contacts to the user
 */
public class ShowContactsActivity extends Activity{
	
	DBAdapter database;
	ArrayList<ContactVO> contactList; 
	ArrayList<String> contacts;
	int identifier = -1;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_emergency_contacts);
		
		Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	identifier = extras.getInt("ID");
        }
        
		getActionBar().setTitle("Emergency Contacts");
		
		database = new DBAdapter(this);
		database.open();
		
		ListView listview = (ListView)findViewById(R.id.ContactListView);
		contacts = new ArrayList<String>();
		if(database.hasContacts()) {
			contactList = database.getContactList();
			for(ContactVO c : contactList) {
				contacts.add(c.getName());
			}
		}
		
		ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				String number = database.getNumberByContactName(contacts.get(position));
				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse("tel:"+number));
				startActivity(callIntent);
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return ContactMenu.setUpContactMenu(menu, this);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
		case R.id.contact_menu_add:
			i = new Intent(ShowContactsActivity.this, CreateContactActivity.class);
			i.putExtra("ID", identifier);
			ShowContactsActivity.this.startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * Called when user clicks the next button on this screen
	 * @param v The View (Button) that was clicked
	 */
	public void onNextClicked(View v) {
		Intent i;
		
		//if this page was accessed after logging in, proceed to home screen
		if (identifier != -1) {
			i = new Intent(ShowContactsActivity.this, HomeScreenActivity.class);
			i.putExtra("ID", identifier);
		}
		//if this page was accessed after logging in, proceed to home screen
		else {
			i = new Intent(ShowContactsActivity.this, LoginActivity.class);
		}
    	ShowContactsActivity.this.startActivity(i);
	}
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}
}
